

import sys
import typing

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QMainWindow, QApplication, QWidget, QLabel
from student_add import Ui_Form
import student_add
import pandas as pd
import sqlite3
import create_tables




class Ui_MainWindow_teacher(QMainWindow):
    def __init__(self):
        super(Ui_MainWindow_teacher, self).__init__()
        self.setWindowFlag(QtCore.Qt.WindowType.MSWindowsFixedSizeDialogHint)
        self.setupUi(self)
        self.actiontianjia1.triggered.connect(self.add_stu)
        self.action1_2.triggered.connect(self.show_logs)  # 绑定日志查询事件
        self.load_db_to_table()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setWindowTitle("宿舍分配系统-教师端")
        MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonFollowStyle)
        self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(parent=MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(parent=self.menubar)
        self.menu.setObjectName("menu")
        self.menu_2 = QtWidgets.QMenu(parent=self.menubar)
        self.menu_2.setObjectName("menu_2")
        self.menu_3 = QtWidgets.QMenu(parent=self.menubar)
        self.menu_3.setObjectName("menu_3")
        self.menu_4 = QtWidgets.QMenu(parent=self.menubar)
        self.menu_4.setObjectName("menu_4")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(parent=MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionxues1 = QtGui.QAction(parent=MainWindow)
        self.actionxues1.setObjectName("actionxues1")
        self.actiontianjia1 = QtGui.QAction(parent=MainWindow)
        self.actiontianjia1.setObjectName("actiontianjia1")
        self.actionxiugai = QtGui.QAction(parent=MainWindow)
        self.actionxiugai.setObjectName("actionxiugai")
        self.actionshanchu = QtGui.QAction(parent=MainWindow)
        self.actionshanchu.setObjectName("actionshanchu")
        self.action1 = QtGui.QAction(parent=MainWindow)
        self.action1.setObjectName("action1")
        self.action2 = QtGui.QAction(parent=MainWindow)
        self.action2.setObjectName("action2")
        self.action1_2 = QtGui.QAction(parent=MainWindow)
        self.action1_2.setObjectName("action1_2")
        self.menu.addAction(self.actiontianjia1)
        self.menu.addAction(self.actionxiugai)
        self.menu.addAction(self.actionshanchu)
        self.menu_3.addAction(self.action1)
        self.menu_3.addAction(self.action2)
        self.menu_4.addAction(self.action1_2)
        self.menubar.addAction(self.menu.menuAction())
        self.menubar.addAction(self.menu_2.menuAction())
        self.menubar.addAction(self.menu_4.menuAction())
        self.menubar.addAction(self.menu_3.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        self.menu.setTitle(_translate("MainWindow", "学生信息管理"))
        self.menu_2.setTitle(_translate("MainWindow", "宿舍信息查询"))
        self.menu_3.setTitle(_translate("MainWindow", "系统设置"))
        self.menu_4.setTitle(_translate("MainWindow", "日志查询"))
        self.actionxues1.setText(_translate("MainWindow", "xues1"))
        self.actiontianjia1.setText(_translate("MainWindow", "添加学生"))
        self.actionxiugai.setText(_translate("MainWindow", "修改学生信息"))
        self.actionshanchu.setText(_translate("MainWindow", "删除学生信息"))
        self.action1.setText(_translate("MainWindow", "安全退出"))
        self.action2.setText(_translate("MainWindow", "关于作者"))
        self.action1_2.setText(_translate("MainWindow", "查看日志"))

    
    def add_stu(self):
        self.m=student_add.Ui_Form()
        self.m.show()
        
    def load_db_to_table(self):

        self.tableWidget = QtWidgets.QTableWidget(parent=self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(20, 10, 751, 500))
        self.tableWidget.setColumnCount(8)

        try:
            conn = sqlite3.connect('student_management.db')
            df = pd.read_sql_query("SELECT * FROM student_info", conn)
            conn.close()
        except Exception as e:
            print(f"数据库连接错误: {e}")

        self.tableWidget.setRowCount(len(df))
        self.tableWidget.setColumnCount(len(df.columns))

        self.tableWidget.setHorizontalHeaderLabels(df.columns)
        

        for row in range(len(df)):
            for col in range(len(df.columns)):
                item = QtWidgets.QTableWidgetItem(str(df.iloc[row, col]))
                self.tableWidget.setItem(row, col, item)
                    
    def show_logs(self):
        try:
            conn = sqlite3.connect('student_management.db')
            df = pd.read_sql_query("SELECT * FROM operation_log", conn)
            conn.close()

            self.tableWidget.setRowCount(len(df))
            self.tableWidget.setColumnCount(len(df.columns))

            self.tableWidget.setHorizontalHeaderLabels(df.columns)

            for row in range(len(df)):
                for col in range(len(df.columns)):
                    item = QtWidgets.QTableWidgetItem(str(df.iloc[row, col]))
                    self.tableWidget.setItem(row, col, item)
        except Exception as e:
            print(f"日志查询错误: {e}")

        


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_MainWindow_teacher()
    ui.show()

    sys.exit(app.exec())
